package com.lastoffer2;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
import org.json.JSONArray;
import org.json.JSONException;
import com.google.gson.Gson;
import com.lastct.dao.Offer;
import com.lastct.dao.OfferTypes;

import android.os.StrictMode;
 
public class JSONConnector {
 
    public static final String URL_SERVICE = "http://10.0.2.2:8080/LAstCTServices/Dispatcher?op=";

	public static List<Offer> getListOffers() {
    	List<Offer> result = null;
//    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//
//    	StrictMode.setThreadPolicy(policy); 
        String jsonString;
		try {
			jsonString = callURL(URL_SERVICE + "listAllOffers");
		
        System.out.println("\n\njsonString: " + jsonString);
 
// Replace this try catch block for all below subsequent examples
       
        	Gson serializator = new Gson();
        	Offer[] jsonResult = serializator.fromJson(jsonString, Offer[].class);
        	result = Arrays.asList(jsonResult);
            JSONArray jsonArray = new JSONArray(jsonString);
            System.out.println("\n\njsonArray: " + jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result  = new ArrayList<Offer>();
			result.add(new Offer());
		}
        return result;
    }
	
	public static List<Offer> getListOffersByType(String type) {
		List<Offer> result = null;
//    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//
//    	StrictMode.setThreadPolicy(policy); 
		String jsonString;
		try {
			jsonString = callURL(URL_SERVICE + "listOffersByType");
			
			System.out.println("\n\njsonString: " + jsonString);
			
// Replace this try catch block for all below subsequent examples
			
			Gson serializator = new Gson();
			Offer[] jsonResult = serializator.fromJson(jsonString, Offer[].class);
			result = Arrays.asList(jsonResult);
			JSONArray jsonArray = new JSONArray(jsonString);
			System.out.println("\n\njsonArray: " + jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result  = new ArrayList<Offer>();
			result.add(new Offer());
		}
		return result;
	}

	public static List<OfferTypes> getListOfferTypes() {
		List<OfferTypes> result = null;
//    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//
//    	StrictMode.setThreadPolicy(policy); 
		String jsonString;
		try {
			jsonString = callURL(URL_SERVICE + "listOfferTypes");
			
			System.out.println("\n\njsonString offerTypes: " + jsonString);
			
// Replace this try catch block for all below subsequent examples
			
			Gson serializator = new Gson();
			OfferTypes[] jsonResult = serializator.fromJson(jsonString, OfferTypes[].class);
			result = Arrays.asList(jsonResult);
			JSONArray jsonArray = new JSONArray(jsonString);
			System.out.println("\n\njsonArray offerTypes: " + jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result  = new ArrayList<OfferTypes>();
			result.add(new OfferTypes());
		}
		return result;
	}
 
    public static String callURL(String myURL) throws IOException {
        System.out.println("Requested URL:" + myURL);
        StringBuilder sb = new StringBuilder();
        URLConnection urlConn = null;
        InputStreamReader in = null;
        
            URL url = new URL(myURL);
            urlConn = url.openConnection();
            if (urlConn != null)
                urlConn.setReadTimeout(60 * 1000);
            if (urlConn != null && urlConn.getInputStream() != null) {
                in = new InputStreamReader(urlConn.getInputStream(),
                        Charset.defaultCharset());
                BufferedReader bufferedReader = new BufferedReader(in);
                if (bufferedReader != null) {
                    int cp;
                    while ((cp = bufferedReader.read()) != -1) {
                        sb.append((char) cp);
                    }
                    bufferedReader.close();
                }
            }
        in.close();
       
        return sb.toString();
    }
 
}